package com.caifu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.caifu.bean.PageDo;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.pojo.YsLabel;
import com.caifu.service.YsLabelService;
import com.caifu.util.Util;
import com.caifu.vo.label.SaveMsLabelVO;
import com.caifu.vo.label.UpdateMsLabelVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName : LabelController
 *
 * @author :  szt
 * @description ：
 * @date : 2020-10-12 14:22
 */
@Controller
@RequestMapping(value = "/sysLabel")
public class SysLabelController {


    @Resource
    private YsLabelService ysLabelService;

    /**
     * 老人档案标签
     *
     * @return Result
     */
    @RequestMapping(value = "jumpSysLabel")
    public ModelAndView jumpUserInfoLabel(String isPension) {
        ModelAndView modelAndView = new ModelAndView();
        try {
            modelAndView.addObject("isPension", Util.null2EmptyTrim(isPension));
            modelAndView.addObject("pensionLabel", ysLabelService.getSysLabelType().getData());
            modelAndView.setViewName("/userInfo/sys_label");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    /**
     * 老人标签 分页
     *
     * @param labelCode 主labelCode
     * @return Result
     */
    @GetMapping(value = "getLabelListByLabelCodePagination")
    @ResponseBody
    public Result<Object> getLabelListByLabelCodePagination(String labelCode,Integer currentPage, Integer pageSize) {
        try {
            Result<Object> result = ysLabelService.getLabelListByLabelCodePagination(labelCode, currentPage, pageSize);
            Result labelInfo =  ysLabelService.getSysLabelByLabelCode(labelCode);
            if(labelInfo!=null){
                PageDo page= (PageDo) result.getData();
                YsLabel tmpLable = (YsLabel) labelInfo.getData();
                final int tmpEditable = tmpLable.getEditable();
                List<YsLabel> labelList = page.getData();
                labelList.forEach(obj->{
                    obj.setEditable(tmpEditable);
                });
            }
            result.setCode(0);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 保存标签名称
     */
    @RequestMapping(value = "saveLabel", method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> saveLabel(SaveMsLabelVO saveMsLabelVO) {
        try {
            return ysLabelService.saveLabel(saveMsLabelVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 修改标签
     */
    @RequestMapping(value = "updateLabel", method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> updateLabel(UpdateMsLabelVO updateMsLabelVO) {
        try {
            return ysLabelService.updateLabel(updateMsLabelVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 删除 标签
     */
    @RequestMapping(value = "deleteLabel", method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> deleteLabel(String labelCode) {
        try {
            return ysLabelService.deleteLogicLabel(labelCode);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 关闭|启动 标签
     */
    @RequestMapping(value = "updateLabelState", method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> updateLabelState(String labelCode, String state) {
        try {
            return ysLabelService.updateLabelState(labelCode,state);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }




}
